<template>
  <div class="app-container">
    <!--<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="xh">
        <el-input
          v-model="queryParams.xh"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制造单位" prop="zzdw">
        <el-input
          v-model="queryParams.zzdw"
          placeholder="请输入制造单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类别" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="注册代码" prop="zcdm">
        <el-input
          v-model="queryParams.zcdm"
          placeholder="请输入注册代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登记证号" prop="djzh">
        <el-input
          v-model="queryParams.djzh"
          placeholder="请输入登记证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="鉴定机构" prop="jdjg">
        <el-input
          v-model="queryParams.jdjg"
          placeholder="请输入鉴定机构"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验日期" prop="checkDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.checkDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择检验日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="复检日期" prop="confirmDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.confirmDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择复检日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效期" prop="validateTerm">
        <el-input
          v-model="queryParams.validateTerm"
          placeholder="请输入有效期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报告编号" prop="reportCode">
        <el-input
          v-model="queryParams.reportCode"
          placeholder="请输入报告编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否过期" prop="expiredFlag">
        <el-input
          v-model="queryParams.expiredFlag"
          placeholder="请输入是否过期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>-->
    <el-row :gutter="10" class="mb8"></el-row>
      <div style="
          font-size: 30px;
          text-align: center;
          margin-bottom: 0px;
          color: #ffffff;
        ">
        青岛崂山矿泉水有限公司特种设备清单
      </div>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        <!-- v-hasPermi="['equipment:tzsbInfo:add']" -->
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleUpdate">修改</el-button>
        <!-- v-hasPermi="['equipment:tzsbInfo:edit']" -->
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
          @click="handleDelete">删除</el-button>
        <!-- v-hasPermi="['equipment:tzsbInfo:remove']" -->
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['equipment:tzsbInfo:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table max-height="560" v-loading="loading" :data="infoList" :cell-style="cellStyle"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="id" align="center" prop="id" />-->
      <!-- <el-table-column label="序号" align="center" prop="xh" /> -->
      <el-table-column label="设备名称" align="center" prop="name" />
      <el-table-column label="制造单位" align="center" prop="zzdw" />
      <el-table-column label="类别" align="center" prop="type" />
      <el-table-column label="注册代码" align="center" prop="zcdm" />
      <el-table-column label="登记证号" align="center" prop="djzh" />
      <el-table-column label="鉴定机构" align="center" prop="jdjg" />
      <el-table-column label="检验日期" align="center" prop="checkDate">
        <template slot-scope="scope">
          <p>{{ parseTime(scope.row.checkDate, "{y}-{m}-{d}") }}</p>
        </template>
      </el-table-column>
      <el-table-column label="复检日期" align="center" prop="confirmDate">
        <template slot-scope="scope">
          <p>{{ parseTime(scope.row.confirmDate, "{y}-{m}-{d}") }}</p>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="validateTerm" />
      <el-table-column label="报告编号" align="center" prop="reportCode" />
      <el-table-column label="是否过期" align="center" prop="expiredFlag" :formatter="hasExpiredFormat" />
      <el-table-column label="剩余天数" align="center" prop="lastDay">
        <template slot-scope="scope">
          <p>{{ scope.row.lastDay }}天</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改特种设备详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-myDialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="xh">
              <el-input v-model="form.xh" placeholder="请输入名称" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入设备名称" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制造单位" prop="zzdw">
              <el-input v-model="form.zzdw" placeholder="请输入制造单位" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别" prop="type">
              <el-input v-model="form.type" placeholder="请输入类别" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册代码" prop="zcdm">
              <el-input v-model="form.zcdm" placeholder="请输入注册代码" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登记证号" prop="djzh">
              <el-input v-model="form.djzh" placeholder="请输入登记证号" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="鉴定机构" prop="jdjg">
              <el-input v-model="form.jdjg" placeholder="请输入鉴定机构" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验日期" prop="checkDate">
              <el-date-picker clearable size="small" v-model="form.checkDate" type="date" value-format="yyyy-MM-dd"
                placeholder="选择检验日期" :disabled="openDisabled">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="复检日期" prop="confirmDate">
              <el-date-picker clearable size="small" v-model="form.confirmDate" type="date" value-format="yyyy-MM-dd"
                placeholder="选择复检日期" :disabled="openDisabled">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期" prop="validateTerm">
              <el-input v-model="form.validateTerm" placeholder="请输入有效期" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12" style="float: left;">
            <el-form-item label="报告编号" prop="reportCode">
              <el-input v-model="form.reportCode" placeholder="请输入报告编号" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item  label="是否过期" prop="expiredFlag">
              <el-input
                v-model="form.expiredFlag"
                placeholder="请输入是否过期"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listInfo,
    getInfo,
    delInfo,
    addInfo,
    updateInfo,
  } from "@/api/equipment/tzsb/info";

  export default {
    name: "Info",
    data() {
      return {
        //弹窗是否禁止编辑
        openDisabled: true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 特种设备详情表格数据
        infoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 创建时间字典
        hasExpiredOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 50,
          xh: null,
          name: null,
          zzdw: null,
          type: null,
          zcdm: null,
          djzh: null,
          jdjg: null,
          checkDate: null,
          confirmDate: null,
          validateTerm: null,
          reportCode: null,
          expiredFlag: this.$route.query.expiredFlag,
          parentId: this.$route.query.parentId,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          xh: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          zzdw: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          zcdm: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          djzh: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          jdjg: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],

          checkDate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          confirmDate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          validateTerm: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          reportCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],

        },
      };
    },
    created() {
      this.getList();
      this.getDicts("has_expired").then((response) => {
        this.hasExpiredOptions = response.data;
      });
    },
    methods: {
      /** 查询特种设备详情列表 */
      getList() {
        this.loading = true;
        listInfo(this.queryParams).then((response) => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 创建时间字典翻译
      hasExpiredFormat(row, column) {
        return this.selectDictLabel(this.hasExpiredOptions, row.expiredFlag);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          xh: null,
          name: null,
          zzdw: null,
          type: null,
          zcdm: null,
          djzh: null,
          jdjg: null,
          checkDate: null,
          confirmDate: null,
          validateTerm: null,
          reportCode: null,
          expiredFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          delFlag: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.openDisabled = false;
        this.open = true;
        this.title = "添加特种设备详情";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.openDisabled = false;
        const id = row.id || this.ids;
        getInfo(id).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "修改特种设备详情";
        });
      },
      /** 查看按钮 */
      handleView(row) {
        this.reset();
        this.openDisabled = true;
        const id = row.id || this.ids;
        getInfo(id).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "查看特种设备详情";
        });
      },

      cellStyle({ row, column, rowIndex, columnIndex }) {
        if (row.lastDay == "0") {
          return "color:red;!important";
        }
        return "";
      },

      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.id != null) {
              updateInfo({ ...this.form, parentId: this.$route.query.parentId }).then((response) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              const rLoading = this.openLoading();
              addInfo({ ...this.form, parentId: this.$route.query.parentId })
                .then((response) => {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  rLoading.close();
                  this.getList();
                })
                .catch((error) => {
                  rLoading.close();
                });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm(
          '是否确认删除数据项?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return delInfo(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "tzsb/info/export",
          {
            ...this.queryParams,
          },
          "特种设备详情" + this.parseTime(new Date()) + ".xlsx"
        );
      },
    },
  };
</script>
